﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Net.Mail;
using System.Net;

namespace ResxCrunch.Business.Managers
{
    /// <summary>
    /// Email related functinality
    /// </summary>
    public class EmailMngr
    {
        #region Constants
        /// <summary>
        /// Instance of the class. Lazy initialization
        /// </summary>
        private static readonly EmailMngr instance = new EmailMngr();

        /// <summary>
        /// Logger declaration
        /// </summary>
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion Constants

        #region Initialization
        /// <summary>
        /// Prevents a default instance of the EmailMngr class from being created.
        /// </summary>
        private EmailMngr()
        {
        }

        /// <summary>
        /// Gets the reference to the object here.
        /// </summary>
        public static EmailMngr Instance
        {
            get
            {
                return instance;
            }
        }
        #endregion Initialization

        /// <summary>
        /// Sends email message based on the data from email and user
        /// </summary>
        /// <param name="fromAddress">From address.</param>
        /// <param name="fromDisplayName">From display name.</param>
        /// <param name="toAddress">To address.</param>
        /// <param name="toDisplayName">To display name.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="host">SMTP host</param>
        /// <param name="port">SMTP port</param>
        /// <returns></returns>
        public bool SendMail(string fromAddress, string fromDisplayName, string toAddress, string toDisplayName, string subject, string body, string host, int port)
        {
            List<string> errorList = new List<string>();

            if (string.IsNullOrEmpty(fromAddress))
            {
                log.Error("'From address' was not set");
                return false;
            }

            if (string.IsNullOrEmpty(toAddress))
            {
                log.Error("'To address' was not set");
                return false;
            }

            if (string.IsNullOrEmpty(subject))
            {
                log.Warn("'Subject' was not set, recurring to empty subject");
                subject = string.Empty;
            }

            if (string.IsNullOrEmpty(body))
            {
                log.Error("'Body' was not set");
                return false;
            }

            try
            {
                // You need to instantiate a new MailMessage class
                MailMessage oMail = new MailMessage();

                // Set the To email address
                oMail.To.Add(new MailAddress(toAddress, string.IsNullOrEmpty(toDisplayName) ? toAddress : toDisplayName));

                // Set the From email address
                oMail.From = new MailAddress(fromAddress, string.IsNullOrEmpty(fromDisplayName) ? fromAddress : fromDisplayName);

                // Set the Subject
                oMail.Subject = subject;

                // You want to allow HTML content and then specify it
                oMail.IsBodyHtml = false;
                oMail.Body = body;

                // This is where you specify the SMTP server and we need to enable SSL on Port 587
                SmtpClient Smtp = new SmtpClient(host, port);
                Smtp.EnableSsl = true;

                // You need to specify your gmail or googlemail email address and password
                Smtp.Credentials = new NetworkCredential("resxcrunch@gmail.com", "3Edcnomcabar");

                // Finally, the following method sends the email
                Smtp.Send(oMail);
            }
            catch (Exception ex)
            {
                log.Error(string.Format("Error while sending email: From: {0} To: {1} Subject:{2} Body:{3}", fromAddress, toAddress, subject, body), ex);
                return false;
            }

            return true;
        }
    }
}
